<script setup lang="ts">
import { reactive, ref, unref } from 'vue'
import { ContentWrap } from '@/components/ContentWrap'
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'

import { required } from '@/utils/formRules'

import { sysDictRelsApi } from '@/api/dict'

// 获取字典
import { useCache } from '@/hooks/web/useCache'

// import { zcPastureListAdminApi } from '@/api/fa/pasture'
import { zcInventoryRuleListApi } from '@/api/cy/xPanrule'
import { zcInventorySeeApi } from '@/api/cy/Pan'
const { wsCache } = useCache()
/****************************** 变量 start ******************************/
const dict: any = wsCache.get('dict')
// 字典通过value获取label
// const getDictLabel = (type, value) => {
//   let data = dict[type].find((item) => {
//     return item.value == value
//   })
//   return data ? data.label : ''
// }

// const userInfo: any = wsCache.get('userInfo')
const data = ref<any>({})
const dictRelsData = ref() // 联动数据
const breedSubData = ref([]) // 品种的枚举

const fileRef = ref()

const rules = reactive({
  owner_type: [required],
  breed: [required],
  pasture_dept_id: [required],
  inventory_rule_id: [required],
  save_marking: [required],
  breed_sub: [required]
})

// const dFile = ref([])
/****************************** 变量 end ******************************/

/****************************** 组件参数 start ******************************/
// 获取参数
const props = defineProps({
  id: {
    type: String,
    default: null
  },
  currentRow: {
    type: Object,
    default: () => null
  }
})
console.log(props, data.value, 'id')
/****************************** 组件参数 end ******************************/

// 调用查看接口获取信息
const getMedicalOrderSee = async () => {
  if (props.id) {
    const res = await zcInventorySeeApi({ id: props.id })
      .catch(() => {})
      .finally(() => {})
    if (res) {
      data.value = await res.data
      // 明细信息处理一下，加上d_id
      // data.value.owner_type = 1
      await methodsForm.setValues(data.value)

      console.log(props.id, data.value, 'res')
    }
  } else {
    data.value.state = '1'
    data.value.selected = []
    data.value.fileData = []
    await methodsForm.setValues(data.value)
  }
}

// 获取联动关系
const getDictRels = async () => {
  const res = await sysDictRelsApi({
    p_type: 'breed',
    c_type: 'breed_sub'
  })
    .catch(() => {})
    .finally(() => {})
  if (res) {
    console.log(res)
    dictRelsData.value = res.data
  }
}

// 根据畜别查询品种breedGetSub
const breedGetSub = async (breed_value) => {
  // console.log('breed_value', breed_value, !breed_value)
  if (!breed_value) {
    breedSubData.value = []
  } else {
    let breedData = dictRelsData.value.find((item) => {
      return item.value == breed_value
    })
    breedSubData.value = breedData.children
  }
}

const schema: any = [
  {
    field: 'owner_type',
    label: '终端类型',
    component: 'Select',
    componentProps: {
      options: dict.inventory_owner_type,
      disabled: props.id ? true : false
    }
  },
  {
    field: 'inventory_rule_id',
    label: '盘点规则名称',
    component: 'DialogRefer',
    componentProps: {
      getListApi: zcInventoryRuleListApi,
      response: { count: 'count', data: 'data' },
      title: '选择盘点规则（双击选择）',
      nameField: 'inventory_rule_name',
      relNameField: 'name',
      valueField: 'id',
      crudSchemas: [
        { field: 'pasture_org_name', label: '所属牧场', search: { show: false } },
        { field: 'name', label: '规则名称', search: { show: true } }
        // { field: 'id', label: '规则id', search: { show: true } }
      ],
      modelName: data.value.inventory_rule_name,
      disabled: props.id ? true : false
    }
  },
  // { component: 'InputNumber', field: 'number', label: '牲畜数量' },
  {
    component: 'DatePicker',
    field: 'inventory_date',
    label: '盘点日期',
    componentProps: {
      valueFormat: 'YYYY-MM-DD'
    }
  },
  // { component: 'InputNumber', field: 'medical_number', label: '就医数量' },
  {
    field: 'note',
    label: '备注',
    component: 'Input',
    colProps: {
      span: 11
    }
  }
]
const {
  register: registerForm,
  methods: methodsForm,
  elFormRef
} = useForm({
  schema: schema
})

const getFormData = async () => {
  let formData = await methodsForm.getFormData()
  // const sku = unref(skuRef)
  // let skuData = sku?.getData()
  const file = unref(fileRef) //附件
  let fileData = file?.getData()
  // const goodsDetails = unref(goodsDetailsRef) //明细
  // let goods_details = goodsDetails?.getData()
  // let goods_tag = getGoodsTag()
  // let goods_details = data.value.goods_details
  // let goods_carousel = data.value.goods_carousel
  return { ...formData, fileData: fileData, selected: data.value.selected }
}

/****************************** 明细配置 start ******************************/

/****************************** 明细配置 end ******************************/

/****************************** 初始化 start ******************************/

const init = async () => {
  // 获取关联关系
  await getDictRels()
  // 调用查看接口查询数据
  await getMedicalOrderSee()
  // 跟新畜别
  if (data.value.breed) {
    await breedGetSub(data.value.breed)
  }
}
init()

defineExpose({
  elFormRef,
  getFormData,
  data
})
/****************************** 初始化 end ******************************/
</script>

<template>
  <div>
    <ContentWrap title="主数据">
      <Form :rules="rules" @register="registerForm" :data="data" />
    </ContentWrap>
  </div>
</template>
